package com.aivin.lib_englishlearn.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.aivin.lib_widget.MaterialButton;
import com.aivin.lib_englishlearn.R;
import com.aivin.lib_englishlearn.SelectCallBack;
import com.aivin.lib_englishlearn.bean.EnglishData;

import java.util.List;
import java.util.Locale;


public class EnglishAdapter extends RecyclerView.Adapter {

    private List<EnglishData> book;
    private SelectCallBack selectCallBack;


    public EnglishAdapter(List<EnglishData> book, SelectCallBack selectCallBack) {
        this.book = book;
        this.selectCallBack = selectCallBack;
        this.setHasStableIds(true);

    }

    @NonNull
    @Override
    public ChooseHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        View view = LayoutInflater.from(context).inflate(R.layout.libenglish_item_txt_choose, parent, false);
        return new ChooseHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        EnglishData bean = book.get(position);
        final ChooseHolder chooseHolder = (ChooseHolder) holder;
        chooseHolder.item_txt.setText(String.format(Locale.getDefault(), "%d、%s", position + 1, bean.getArticleName()));
    }


    @Override
    public int getItemCount() {
        return book == null ? 0 : book.size();
    }


    @Override
    public long getItemId(int position) {
        return position;
    }


    private class ChooseHolder extends RecyclerView.ViewHolder {
        private MaterialButton item_txt;
        private TextView readCountTv;

        private ChooseHolder(View itemView) {
            super(itemView);
            item_txt = itemView.findViewById(R.id.item_txt);
            readCountTv = itemView.findViewById(R.id.readCountTv);
            readCountTv.setVisibility(View.INVISIBLE);

            View.OnClickListener clickListener = v -> {
                int mSelectedItem = getAdapterPosition();
                selectCallBack.onSelected(book.get(mSelectedItem), mSelectedItem);
                notifyDataSetChanged();
            };

            item_txt.setOnClickListener(clickListener);

        }
    }


}
